// Copyright (c) Microsoft Corporation. All rights reserved.

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace Microsoft.Tools.CodeRunner
{
	/// <summary>
	/// Extends the functionality of <see cref="System.IO.Path"/>. 
	/// </summary>
	public sealed class PathUtility
	{
		#region Constructors
		// No need to construct this object
		private PathUtility()
		{
		}
		#endregion
		
		/// <summary>
		/// Prefix for UNC file paths. 
		/// </summary>
		public static readonly string UncPrefixChars = @"\\";
		
		/// <summary>
		/// Character used to separate the file extension from the main part of the file name.
		/// </summary>
		public static readonly char ExtensionSeparatorChar = '.';

		/// <summary>
		/// Searches multiple directories for a file.
		/// </summary>
		/// <param name="paths">An array of paths to search.  Each path is assumed to be a directory.</param>
		/// <param name="file">The file to search for.  The file path can be relative, but wildcards are not allowed.</param>
		/// <returns>An array of just the paths that contain the file, in the same order as the passed in array.</returns>
		public static string[] FindFileInPaths(string[] paths, string file)
		{
			ArrayList newPaths = new ArrayList();

			// TODO: Check for invalid characters (wildcards) in the file name
			
			foreach (string path in paths)
			{
				try 
				{
					string fullPath = 
						new ParsedPath(path, PathType.Directory).RootAndDirectory + 
						new ParsedPath(file, PathType.File).FileAndExtension;
				
					if (File.Exists(fullPath))
						newPaths.Add(fullPath);
				}
				catch (ArgumentException)
				{
					// If there is a bad path in the list, ignore it
				}
			}
			
			return (string[])newPaths.ToArray(typeof(string));
		}
	}
}
